package com.lancoo.znbkxx.teacher.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.ClassKnowledgeAreaInfor
import kotlinx.android.synthetic.main.aibk_adapter_item_knowledge_area.view.*

class AibkKnowledgeAreaAdapter(
        val context: Context,
        val classKnowledgeAreaInfor: ClassKnowledgeAreaInfor

): KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View? {
        return LayoutInflater.from(context)
                .inflate(R.layout.aibk_adapter_item_knowledge_area, parent, false)
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {

        val name = classKnowledgeAreaInfor[position].Name
        val firstExplain = classKnowledgeAreaInfor[position].FirstExplain
        val sccondExplain = classKnowledgeAreaInfor[position].SccondExplain
        val isSelected = classKnowledgeAreaInfor[position].isSelected

        holder.convertView.TvKnowledgeAreaName.text = name
        holder.convertView.TvFirstExplain.text = firstExplain
        holder.convertView.TvSccondExplain.text = sccondExplain
        holder.convertView.IvSelectedState.isSelected = isSelected
        holder.convertView.IvSelectedState.setOnClickListener {
            val inforSize = classKnowledgeAreaInfor.size
            for(i in 0 until inforSize){
                classKnowledgeAreaInfor[i].isSelected = i == position
            }

            clickListener!!.onClickWhich(position)
            notifyDataSetChanged()
        }
    }

    override fun getItemCount(): Int {
        return classKnowledgeAreaInfor.size
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    interface OnClickListener {
        fun onClickWhich(pos:Int)
    }

    private var clickListener: OnClickListener? = null

    fun setClickListener(clickListener: OnClickListener?) {
        this.clickListener = clickListener
    }

}